﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using mealplanner.control;


namespace mealplanner.View
{

    

    public partial class Menue1 : Form
    {
        string type = "";
        int from=0;
        int to=0;

        public Menue1(bool diet,int from,int to)
        {
            InitializeComponent();
            viewmeal();

            if (diet)
            {
                MessageBox.Show("Diet From : "+from+"| To : "+to+"");
                type = "diet";
                this.from = from;
                this.to = to;
                view_DietMeals();
            }
            else {
                view_healthyMeal();
                type = "normal";
            }
        }

        void view_DietMeals() {
            Diet diet = new Diet();
            ArrayList dietMeals=new ArrayList();
            dietMeals = diet.MealsForSpecificCaloriesRange(from, to);

            TreeNode healthy = new TreeNode();
            healthy.Text = "Diet Meals";


            foreach (string meal in dietMeals)
            {
                //MessageBox.Show(meal);
                healthy.Nodes.Add(meal);
            }

            mainMenueList.Nodes.Add(healthy);

        }

        void view_healthyMeal() {
            Normal normal = new Normal();
            ArrayList healthyMeal = new ArrayList();
            healthyMeal = normal.getHealthyMeal();
            
            TreeNode healthy=new TreeNode();
            healthy.Text="Healthy Meal";


            foreach(string meal in healthyMeal){
               // MessageBox.Show(meal);
                healthy.Nodes.Add(meal);
            }

            mainMenueList.Nodes.Add(healthy);

        }

        ArrayList Menue = new ArrayList();


        void viewmeal() {
            ArrayList Groups = new ArrayList();
            Meal m1 = new Meal();
            Groups.Add("Dish");
            Groups.Add("Sandwich");
            Groups.Add("Soup");
            Groups.Add("Juice");
            Groups.Add("Salad");

           

            for (int i = 0; i < 5; i++)
            {

                ArrayList temp_list = m1.ShowMeals(Groups[i].ToString());

                foreach (string meal in temp_list)
                {
                    TreeNode x = new TreeNode();
                    x.Text = meal;
                    x.NodeFont = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            
                    this.mainMenueList.Nodes[i].Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                x
                    });

                   
                }
            }
        }

        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeView x =(TreeView) sender;


            for (int i = 0; i < mainMenueList.Nodes.Count; i++)
            {
                for (int y = 0; y < x.Nodes[i].Nodes.Count;y++)
                {
                    if(x.Nodes[i].Nodes[y].IsSelected){
                        Details form = new Details(x.Nodes[i].Nodes[y].Text);
                        form.Show();
                        //this.Visible = false;
                    
                    }
                }
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {

            menu menu = new menu();
            ArrayList Menues = new ArrayList();
            Menues = menu.retriveAllMenu();

            foreach (string menue in Menues){
                menuesList.Items.Add(menue);
            }
            
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            mainMenueList.DoDragDrop(e.Item, DragDropEffects.Move);
            
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TreeNode)))
            {
                TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
                string pattern;
                pattern = @"^Dish$|^Sandwish$|^Juice$|^soup$|^salad$|^Diet Meals$|^Healthy Meal$";
                if (!Regex.IsMatch(tn.Text, pattern))
                {

                    MessageBox.Show("ahmed");
                    
                    Menue.Add(tn.Text);

                    foreach (string meal in Menue)
                    {
                        MessageBox.Show(meal);
                    }


                    menu menu = new menu();



                    int valid = menu.checkStructure(Menue);

                    

                    if (valid == 1)
                    {
                        menueView.Items.Add(tn.Text, tn.ImageIndex);
                     //   Menue.Add(tn.Text);

                        MessageBox.Show("kop");

                    }
                    else
                    {

                        Menue.Remove(tn.Text);
                        if (valid == -1)
                        {
                            MessageBox.Show("Can't Add Day Befor Week ! ");
                        }
                        else
                        {
                            MessageBox.Show("Already Exist");
                        }

                    }

                }
                //     treeView1.Nodes.Remove(tn);
            }

        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            // this code can be in DragOver also
            if (e.Data.GetDataPresent(typeof(TreeNode)))
                e.Effect = DragDropEffects.Move;
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String Text = searchInput.Text; // string to search for

            if(Text==""){
                MessageBox.Show("Please Enter String");
            }else{

                Meal meal = new Meal();
                ArrayList SearchResult = new ArrayList();

                
            if (mealChoice.Checked) {
                SearchResult=meal.searchMealByName(Text);

            }
            else if (ingChoice.Checked) {
                SearchResult = meal.SearchMealsByIngredients(search_ing);
            
            }
            else { MessageBox.Show("Please Choose Search Type"); }

            if (mealChoice.Checked || ingChoice.Checked) {

                TreeNode result = new TreeNode();
                result.Name = "Result";
                result.Text = "Search Result";
                try
                {
                    int i = 5;
                    if (type == "normal")
                    {
                        i = 6;
                    }

                    mainMenueList.Nodes.RemoveAt(i);

                }
                catch {  }

                foreach (string x in SearchResult)
                {
                    TreeNode subResult = new TreeNode();
                    subResult.Text = x;

                    result.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                        subResult
                    });
                }

                mainMenueList.Nodes.Add(result);
               
            }

            

                
            }
            

        }

        //private MouseEventHandler MouseEventHandler(Action<object, MouseEventArgs> action)
        //{
        //    string name=
        //}

        

        private void button2_Click(object sender, EventArgs e)
        {
            //View Meal Planer Data 

 
            try
            {
                
                menueView.Clear();

                // function to get meal planer from DataBase

                string selectedMenu = menuesList.SelectedItem.ToString();

                menu menue = new menu();

                ArrayList MenueDetails = new ArrayList();
                MenueDetails=menue.retriveMenu(selectedMenu);

                foreach(string x in MenueDetails)
                    menueView.Items.Add(x);
               // menueView.Enabled = false;


                
            }
            catch
            {
              //  MessageBox.Show("Please Choose Day & Week ");

            }



        }

        private void treeView2_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ing_list.DoDragDrop(e.Item, DragDropEffects.Move);
            
        }
        bool Deleting=false;

        private void DeleteButton_Click(object sender, EventArgs e)
        {

            if (!Deleting)
            {
                
                // menueView.CheckBoxes = true;
                DeleteButton.Text="Done";
                Deleting = true;
                menue_selection.Show();
                delete_item_selection.Show();

            }
            else
            {
               
                int menue_Index = menuesList.SelectedIndex;
                
                //MessageBox.Show(menue_Index.ToString());
                
                if (menue_Index != -1)
                {
                    if (menue_selection.Checked)
                    {
                
                        MessageBox.Show("meal Deleted");
                        menueView.Clear();
                        menu menue = new menu();
                        menue.deleteMenu(menuesList.SelectedItem.ToString());
                        menuesList.Items.RemoveAt(menue_Index);
                        

                    }
                    else if (delete_item_selection.Checked)
                    {

                        ArrayList Deleted_Items = new ArrayList();

                        
                        int count = menueView.CheckedItems.Count;

                        for (int i = 0; i < count; i++)
                        {
                            //MessageBox.Show(menueView.CheckedItems[0].Text);
                            string meal = menueView.CheckedItems[0].Text;
                            menueView.CheckedItems[0].Remove();
                            Deleted_Items.Add(meal);
                            Menue.Remove(meal);

                        }

                        menu menue = new menu();

                        menue.deleteSpecificDetailsFromMenu(menuesList.SelectedItem.ToString(), Deleted_Items);

                        menueView.CheckBoxes = false;

                    }
                    else
                    {
                        MessageBox.Show("Please Select Choice !");
                    }

                    if (menue_selection.Checked || delete_item_selection.Checked)
                    {
                        Deleting = false;
                        DeleteButton.Text = "Delete";
                        menue_selection.Hide();
                        delete_item_selection.Hide();

                    }
                }
                else
                {
                    MessageBox.Show("Plaease Select meal");
                }

            }
        }

        bool Adding = false;

        private void button3_Click(object sender, EventArgs e)
        {
            if (ing_list.Visible) { ing_list.Hide(); }

            menueView.Enabled = true;
            menueView.Clear();
            
            if (!Adding)
            {
                Adding = true;
                timeList.Show();
                textBox2.Show();
                DeleteButton.Show();
                addMenueButton.Text = "Done";
                menueView.AllowDrop=true;

                DeleteButton.Show();
                menue_selection.Hide();
                delete_item_selection.Hide();

                deleteItem.Show();

            }
            else
            {

                // Send Menue 

                String menueName = textBox2.Text;
                // Menue -- Array List


                if (menueName == "")
                {
                    MessageBox.Show("plaease Enter MENUE Name ! ");
                }
                else
                {
                    menu menue = new menu();
                    menue.setname(menueName);
                    menue.add_menu(Menue);

                    Adding = false;
                    timeList.Hide();
                    textBox2.Hide();
                    DeleteButton.Hide();
                    addMenueButton.Text = "Add Menue";

                    menueView.Clear();
                    Menue.Clear();

                    menuesList.Items.Add(menueName.ToString());
                    menueView.AllowDrop = false;
                    textBox2.Text = "";
                }
            }

        }

        private void listView1_DragEnter_1(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TreeNode)))
                e.Effect = DragDropEffects.Move;

        }

        private void listView1_DragDrop_1(object sender, DragEventArgs e)
        {
            

            if (e.Data.GetDataPresent(typeof(TreeNode)))
            {
                TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
               if(!ings.Contains(tn.Text.ToString())){
                string pattern;
                pattern = @"^Dish$|^sandwish$|^Juice$|^soup$|^salad$";
                if (!Regex.IsMatch(tn.Text, pattern))
                {

                 //   MessageBox.Show("ahmed");

                    Menue.Add(tn.Text);

                    foreach (string meal in Menue)
                    {
                   //     MessageBox.Show(meal);
                    }


                    menu menu = new menu();



                    int valid = menu.checkStructure(Menue);



                    if (valid == 1)
                    {
                        menueView.Items.Add(tn.Text, tn.ImageIndex);
                        //Menue.Add(tn.Text);
                        //MessageBox.Show("kop");
                        if (!Adding)
                        {
                            ArrayList added = new ArrayList();
                            added.Add(tn.Text);
                            menu m1 = new menu();
                            m1.updateMenu(menuesList.SelectedItem.ToString(), added);
                        }
                    }
                    else
                    {

                        Menue.Remove(tn.Text);
                        if (valid == -1)
                        {
                            MessageBox.Show("Can't Add Day Befor Week ! ");
                        }
                        else
                        {
                            MessageBox.Show("Already Exist");
                        }

                    }

                }
               }
                //     treeView1.Nodes.Remove(tn);
            }

        }

        bool ing_loaded = false;
        ArrayList ings = new ArrayList();
                
        private void ingChoice_CheckedChanged(object sender, EventArgs e)
        {
            

            if (!ing_loaded){
                
                Meal get_ing=new Meal();

                
                 // call function that return all ingrdients and assign value to ings
                ings = get_ing.getAllIngredient();

                foreach (string ing in ings) {
                    ing_list.Nodes[0].Nodes.Add(ing.ToString());
                }
                ing_loaded = true;

            }

            if (timeList.Visible) { timeList.Hide(); }
            ing_list.Show();
        }

        private void searchInput_DragEnter(object sender, DragEventArgs e)
        {
            
            if (e.Data.GetDataPresent(typeof(TreeNode)))
                e.Effect = DragDropEffects.Move;
            

        }

        ArrayList search_ing = new ArrayList();
        private void searchInput_DragDrop(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(typeof(TreeNode)))
            {
                TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
                if (ings.Contains(tn.Text))
                {
                    if (!search_ing.Contains(tn.Text.ToString()))
                    {

                        searchInput.Text += tn.Text + " | ";
                        // treeView1.Nodes.Remove(tn);
                        search_ing.Add(tn.Text.ToString());
                    }
                    else
                    {
                        MessageBox.Show("This Ingredient Already added in search field. ");
                    }

                }
                else {
                    MessageBox.Show("Only Ingredients can Be Dropped ! ");
                }
            }

        }

        private void mealChoice_CheckedChanged(object sender, EventArgs e)
        {
            ing_list.Hide();
            
        }

        private void menuesList_SelectedIndexChanged(object sender, EventArgs e)
        {
           // EditeButton.Show();
            DeleteButton.Show();

            try
            {
                timeList.Show();

                menueView.AllowDrop = true;

                menueView.Clear();

                // function to get meal planer from DataBase

                string selectedMenu = menuesList.SelectedItem.ToString();

                menu menue = new menu();

                ArrayList MenueDetails = new ArrayList();
                MenueDetails = menue.retriveMenu(selectedMenu);

               foreach (string x in MenueDetails)
                    menueView.Items.Add(x);
                // menueView.Enabled = false;



            }
            catch
            {
                //  MessageBox.Show("Please Choose Day & Week ");

            }

        }

        private void delete_item_selection_CheckedChanged(object sender, EventArgs e)
        {
            menueView.CheckBoxes = true;
        }

        private void menueView_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            /*
            try
            {
                bool check = false;


                string pattern;
                pattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$";
               

                string name = sender.ToString();

                int number = (int)menueView.CheckedItems.Count - 1;

                int item_index = menueView.CheckedItems[number].Index;
                
                if(menueView.Items[number].Checked){
                    check = true;
                }

                string item_name = menueView.CheckedItems[number].Text.ToString();

                MessageBox.Show(item_index.ToString());

                if (Regex.IsMatch(item_name, pattern))
                {

                    int i = item_index+1;
                    MessageBox.Show(i.ToString());

                    while (i<menueView.Items.Count)
                    {
                        
                        if (Regex.IsMatch(menueView.Items[i].Text, pattern))
                        {
                            MessageBox.Show("break");

                            break;
                        }
                        
                        {
                            menueView.Items[i].Checked = true;
                            
                            MessageBox.Show(menueView.Items[i].Text);

                        }
                        i++;
                    }

                }
                else {
                    MessageBox.Show("not matched");
                }

                

            }
            catch{
                MessageBox.Show("first selection");
            }

            */
        }

        private void ing_list_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
        bool delete_flag = false;

        private void deleteItem_Click(object sender, EventArgs e)
        {
            if (!delete_flag)
            {
                deleteItem.Text = "Done";
                menueView.CheckBoxes = true;
                delete_flag = true;

            }
            else {

                int number=menueView.CheckedItems.Count;
                int i=0;
                while(i<number){
                    menueView.CheckedItems[0].Remove();
                    i++;
                }
            
            }
        }

        private void mealPlannerBox_Enter(object sender, EventArgs e)
        {

        }


        
    }
}
